#include <iostream>
#include <fstream>
#include "Contact.pb.h"

using namespace std;
using namespace contacts;

void PrintContacts(const Contacts& contacts)
{
    for(int i = 0;  i < contacts.contacts_size(); i++)
    {
        const PeopleInfo& people = contacts.contacts(i);
        cout << "------------联系⼈" << i+1 << "------------" << endl;
        cout << "姓名：" << people.name() << endl;
        cout << "年龄：" << people.age() << endl;

        int j = 0;
        for(const PeopleInfo_Phone& phone : people.phone())
        {
            cout << "电话" << j++ << ": " << phone.number() << endl;
        } 
    }
}

int main(int argc,char* argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    
    if (argc != 2) {
    cerr << "Usage: " << argv[0] << "CONTACTS_FILE" << endl;
    return -1;
    }

    // 以⼆进制⽅式读取 contacts
    Contacts contacts;

    fstream input(argv[1], ios::in | ios::binary);

    if(!contacts.ParseFromIstream(&input))
    {
        cerr << "Failed to parse contacts." << endl;
        input.close();
        return -1;
    }
    
    PrintContacts(contacts);
    
    input.close();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;

}